package network.t_01_bio;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class BIOClient {
    public static void main(String[] args) throws IOException {
        handler3();
    }

    static void handler1() { // 一次性发送接收
        try (Socket socket = new Socket("127.0.0.1", 6666);
             InputStream is = socket.getInputStream();
             OutputStream os = socket.getOutputStream();
             ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i <= 9999999; i++) sb.append("Hello：").append(i).append("，");
            os.write(sb.toString().getBytes(StandardCharsets.UTF_8));
            os.flush();
            socket.shutdownOutput();
            System.out.println("发送完毕");

            int length;
            byte[] buffer = new byte[4096];
            while ((length = is.read(buffer)) != -1) baos.write(buffer, 0, length); // 阻塞当前线程，等待对方发送数据
            socket.shutdownInput();
            System.out.println(baos.toString(StandardCharsets.UTF_8.name()) + "\n收到数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void handler2() { // 持续发送接收，用 \n 标识是否传输完毕
        try (Socket socket = new Socket("127.0.0.1", 6666);
             InputStream is = socket.getInputStream();
             OutputStream os = socket.getOutputStream();
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             InputStreamReader isr = new InputStreamReader(System.in)) {
            StringBuilder sb = new StringBuilder();
            int length;
            byte[] buffer = new byte[4096];
            while (isr.read() != 'q') {
                sb.setLength(0);
                for (int i = 0; i <= 9999999; i++) sb.append("Hello：").append(i).append("，");
                os.write(sb.append('\n').toString().getBytes(StandardCharsets.UTF_8));
                os.flush();
                System.out.println("发送完毕");

                while ((length = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, length); // 阻塞当前线程，等待客户端发送数据
                    if (buffer[length - 1] == '\n') break;
                }
                System.out.println(baos.toString(StandardCharsets.UTF_8.name()) + "\n收到数据");
                baos.reset();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void handler3() { // TLV(Tag-Length-Value)
        try (Socket socket = new Socket("127.0.0.1", 6666);
             DataInputStream dis = new DataInputStream(socket.getInputStream());
             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
             InputStreamReader isr = new InputStreamReader(System.in)) {
            StringBuilder sb = new StringBuilder();
            while (isr.read() != 'q') {
                sb.setLength(0);
                for (int i = 0; i <= 9999999; i++) sb.append("Hello：").append(i).append("，");
                dos.writeUTF("tag"); // 发送标签
                dos.writeInt(sb.toString().getBytes(StandardCharsets.UTF_8).length); // 发送长度
                dos.write(sb.toString().getBytes(StandardCharsets.UTF_8)); // 发送数据
                dos.flush(); // 刷新缓冲区
                System.out.println("发送完毕！");

                String tag = dis.readUTF(); // 接收标签
                byte[] bytes = new byte[dis.readInt()];// 接收长度
                dis.readFully(bytes); // 接收数据
                System.out.println(new String(bytes, StandardCharsets.UTF_8) + "\n收到数据");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}